//
//  HomeCityController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/18.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeCityController.h"
#import "HomeRecomedController.h"
#import "HomeQuestionAndAnswerController.h"
#import "HomeFocusCell.h"
#import "ChooseCityController.h"
#import "HomeCityModel.h"

@interface HomeCityController ()<UITableViewDelegate,UITableViewDataSource,ChooseCityDelegate>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**headerView*/
@property (nonatomic,strong)UIView*headerView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation HomeCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section == 0) {
//        return 3;
//    }
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecomedLeftTextRightImageCell * imageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
    MoreImageAndTextCell * moreCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MoreImageAndTextCell class])];
    HomeFocusMorenImageCell * userMoreImageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];

    HomeCityModel * model = self.dataArray[indexPath.row];
    
    if (model.imageNumber.integerValue == 3) {
        moreCell.homeCityModel = model;
        return moreCell;
    }
    if (model.imageNumber.integerValue == 1) {
        imageCell.homeCityModel  = model;
        return imageCell;
    }
    
    userMoreImageCell.homeCityModel  = model;

    return userMoreImageCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController pushViewController:[ImageAndTextDetailController new] animated:YES];
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.tableFooterView = UIView.new;
        _listView.tableHeaderView = self.headerView;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;
        _listView.separatorColor = SeparatorColor;
        
        [_listView registerClass:[RecomedLeftTextRightImageCell class] forCellReuseIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
        
        [_listView registerClass:[MoreImageAndTextCell class] forCellReuseIdentifier:NSStringFromClass([MoreImageAndTextCell class])];
        [_listView registerClass:[HomeFocusMorenImageCell class] forCellReuseIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];

        
    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeCity" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeCityModel *model = [HomeCityModel new];
            [model setValuesForKeysWithDictionary:obj];
            
            if (model.imageNumber.integerValue > 0) {
                NSMutableArray * imageArray = [NSMutableArray array];
                for (NSInteger i = 0 ; i < model.imageNumber.integerValue; i++) {
                    [imageArray addObject:IMAGE_NAMED([Utilis getRandomDynamicImage])];
                }
                model.userImage = [Utilis getRandomUserImage];
                model.imageArray = imageArray;
            }
            
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}
-(UIView*)headerView{
    if(!_headerView){
        _headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 36)];
        _headerView.backgroundColor = KWhiteColor;
        _headerView.userInteractionEnabled = YES;
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"点击选择其他城市" forState:UIControlStateNormal];
        [button setImage:IMAGE_NAMED(@"home_loction") forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x454F44) forState:UIControlStateNormal];
        button.titleLabel.font = SYSTEMFONT(14);
        
        kWeakSelf(weakSelf);
        [button addActionHandler:^{
            [weakSelf onClickChooseCity];
        }];
        
        [_headerView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
    }
    return _headerView;
}
- (void)onClickChooseCity {
    
    ChooseCityController *cityPickerVC = [[ChooseCityController alloc] init];
    [cityPickerVC setDelegate:self];
    
        cityPickerVC.locationCityID = @"1400010000";
    // 最近访问城市，如果不设置，将自动管理
    cityPickerVC.hotCitys = @[@"100010000", @"200010000", @"300210000", @"600010000", @"300110000"];
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
        
    }];
}
#pragma mark - CityPickerDelegate
- (void) cityPickerController:(ChooseCityController *)chooseCityController didSelectCity:(CityObject *)city
{
//    [chooseCityBtn setTitle:city.cityName forState:UIControlStateNormal];
    DLogFunc;
    BlockSafeRun(self.selecteBlock,city.cityName);
    [chooseCityController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void) cityPickerControllerDidCancel:(ChooseCityController *)chooseCityController{
    DLogFunc;
    [chooseCityController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
